package com.yunc.user.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.common.core.result.R;
import com.yunc.user.sys.entity.SysDict;
import com.yunc.user.sys.entity.SysDictData;
import com.yunc.user.sys.mapper.SysDictMapper;
import com.yunc.user.sys.service.ISysDictDataService;
import com.yunc.user.sys.service.ISysDictService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典表
 *
 * @author congcong tao
 * @date 2018/11/19 17:08
 */
@Service("sysDictService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    private final SysDictMapper sysDictMapper;
    private final ISysDictDataService sysDictDataService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R updateDict(SysDict dict) {
        SysDict sysDict = this.getById(dict.getId());
        // 原字典类型
        String dictType = sysDict.getDictType();
        boolean flag = this.updateById(dict);
        if (!flag) {
            return R.error();
        }
        // 如果不一致
        if (!StringUtils.equals(dictType, dict.getDictType())) {
            QueryWrapper<SysDictData> qw = new QueryWrapper<>();
            qw.eq("dict_type", dictType);
            List<SysDictData> list = sysDictDataService.list(qw);
            if (CollUtil.isNotEmpty(list)) {
                for (SysDictData sysDictData : list) {
                    sysDictData.setDictType(dict.getDictType());
                }
                // 批量修改
                sysDictDataService.updateBatchById(list);
            }
        }
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R removeDict(String ids) {
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        Collection<SysDict> sysDicts = this.listByIds(idsList);
        if (CollUtil.isEmpty(sysDicts)) {
            return R.error("未查询到数据");
        }
        boolean flag = this.removeByIds(idsList);
        if (!flag) {
            return R.error();
        }
        // 删除关联数据
        List<String> dictDataIds = new ArrayList<>();
        for (SysDict sysDict : sysDicts) {
            QueryWrapper<SysDictData> qw = new QueryWrapper<>();
            qw.eq("dict_type", sysDict.getDictType());
            List<SysDictData> list = sysDictDataService.list(qw);
            List<String> resultList = list.stream().map(SysDictData::getId).collect(Collectors.toList());
            dictDataIds.addAll(resultList);
        }
        if (CollUtil.isEmpty(dictDataIds)) {
            return R.ok();
        }
        boolean flagData = sysDictDataService.removeByIds(dictDataIds);
        if (!flagData) {
            return R.error();
        }
        return R.ok();
    }
}
